package my.algorithms.mcorrea.topcoder;

import java.util.HashMap;
import java.util.Map;

/**
 * http://community.topcoder.com/stat?c=problem_statement&pm=11867
 *
 * @author mcorrea
 */
public class PasswordXGuessing {

	public static void main(String[] args) {
		System.out.println( howMany(new String[]{"58", "47"}) );
		System.out.println(howMany(new String[]{"539", "540", "541"}));
		System.out.println( howMany(new String[]{"12", "34", "56", "78"}) );
		System.out.println( howMany(new String[]{"2", "3", "5"}) );
		System.out.println( howMany(new String[]{"4747", "4747", "4747", "4747"}) );
	}

	public static long howMany(String[] guesses) {

		Map<String, Integer> possibles = new HashMap<String, Integer>();

		//get possibles password with first ocurrence
		for(String guess: guesses){
			for (int k = 0; k < guess.length(); k++) {
				String t1 = "";
				for (int c = 0; c <= 9; c++) {

					if (k > 0) {
						t1 = guess.substring(0, k) + c + guess.substring(k + 1, guess.length());
					} else {
						t1 = c + guess.substring(k + 1, guess.length());
					}

					if (possibles.get(t1) == null) {
						possibles.put(t1, 1);
					} else {
						possibles.put(t1, possibles.get(t1) + 1);
					}
				}
			}
		}

		for(String guess: guesses){
			possibles.remove(guess);
		}
		
		int count = 0;
		for (String key : possibles.keySet()) {
			if (possibles.get(key) == guesses.length ) {
				count++;
				System.out.println("--> " + key);
			}
			//System.out.println("--> " + key + ": " + possibles.get(key));
		}

		return count;
	}
}
